home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / X-Ray / samples / transparent floater / source / init.c < prev    next >
Encoding:
Text File  |  1999-06-09  |  3.0 KB  |  130 lines  |  [TEXT/CWIE]

  1. // Copyright (C) 1999 Eric Roccasecca.  All rights reserved.
  2.  
  3. #include "init.h"
  4. #include "X_Ray.h"
  5.  
  6. #define kTransparencyOpaque            0x0000
  7. #define kTransparencyClear            0xFFFF
  8.  
  9. OSErr                    gErr = noErr;
  10. flt_GlobalCommRec        gCommRec;
  11. GetNextEventFilterUPP    gOldjGNEFilter = nil,
  12.                         gGNEFilterUPP = nil;
  13. SelectorFunctionUPP        gGestaltUPP;
  14. RgnHandle                gCheckRgn = nil;
  15.  
  16.  
  17. void main (void)
  18. {
  19.     Handle    theCode;
  20.     THz        savedZone;
  21.     
  22.     savedZone = GetZone();  // make sure we are in a safe memory zone
  23.     SetZone (SystemZone());
  24.     
  25.     gCommRec.daemonPSN.lowLongOfPSN = 0;
  26.     gCommRec.daemonPSN.highLongOfPSN = 0;
  27.     gCommRec.daemonWindow = nil;
  28.     gCommRec.daemonZone = 0;
  29.     gCommRec.daemonQDGlobals = nil;
  30.     gCommRec.daemonWindow = nil;
  31.     
  32.     if (InstalStuff())
  33.     {
  34.         // keep us around after INIT file is closed
  35.         theCode = Get1Resource ('INIT', 128);
  36.         DetachResource (theCode);
  37.         
  38.         gCheckRgn = NewRgn();
  39.     }
  40.     
  41.     SetZone (savedZone);    // restore memory zone
  42. }
  43.  
  44.  
  45. // passes back to caller a pointer to the global communication rec
  46. pascal OSErr flt_Gestalt (OSType selector, long *response)
  47. {
  48. #pragma unused (selector, response)
  49.     *response = (long)&gCommRec;
  50.     return noErr;
  51. }
  52.  
  53.  
  54. OSErr IsDaemonRunning (void)
  55. {
  56.     ProcessInfoRec            info;
  57.     
  58.     info.processInfoLength = sizeof(ProcessInfoRec);
  59.     info.processName = nil;
  60.     info.processAppSpec = nil;
  61.     return GetProcessInformation (&gCommRec.daemonPSN, &info);
  62. }
  63.  
  64.  
  65. Boolean InstalStuff (void)
  66. {
  67.     gGestaltUPP = NewSelectorFunctionProc (flt_Gestalt);
  68.     gErr = NewGestalt (kfltGestalt, gGestaltUPP);
  69.     if (gErr)
  70.         return false;
  71.     
  72.     // Install GNE filter routine
  73.     gOldjGNEFilter = LMGetGNEFilter();
  74.     gGNEFilterUPP = NewGetNextEventFilterProc (flt_GNEFilter);
  75.     LMSetGNEFilter ((GNEFilterUPP)gGNEFilterUPP);
  76.     
  77.     return true;
  78. }
  79.  
  80.  
  81. // handles all events regarding the floating windows
  82. pascal void flt_GNEFilter (EventRecord *theEvent, Boolean *result)
  83. {
  84.     WindowPtr    foundWin;
  85.     short        foundWinPart;
  86.     
  87.     if (gOldjGNEFilter)
  88.         CallGetNextEventFilterProc (gOldjGNEFilter, theEvent, result);
  89.     
  90.     if (gCommRec.daemonWindow && (IsDaemonRunning() == noErr))
  91.     {
  92.         // check for mouse related happenings
  93.         UnionRgn (((WindowPeek)gCommRec.daemonWindow)->strucRgn, ((WindowPeek)gCommRec.daemonWindow)->contRgn, gCheckRgn);
  94.         if (PtInRgn (theEvent->where, gCheckRgn))
  95.         {
  96.             RgnHandle            checkRgn = nil;
  97.             
  98.             foundWinPart = FindServiceWindow (theEvent->where, &foundWin);
  99.             if (foundWin)
  100.             {
  101.                 switch (theEvent->what)
  102.                 {
  103.                     case mouseDown:
  104.                         if (foundWin == gCommRec.daemonWindow)
  105.                         {
  106.                             switch (foundWinPart) {
  107.                                 case inDrag:
  108.                                     DragWindow (foundWin, theEvent->where, &(*LMGetGrayRgn())->rgnBBox);
  109.                                     break;
  110.                                 case inGoAway:
  111.                                     gCommRec.daemonCloseWindow = TrackGoAway (foundWin, theEvent->where);
  112.                                     if (gCommRec.daemonCloseWindow)
  113.                                         gErr = WakeUpProcess (&gCommRec.daemonPSN);
  114.                                     break;
  115.                                 default:
  116.                                     break;
  117.                             }
  118.                             
  119.                             // event has been handled, don't pass it on
  120.                             theEvent->what = nullEvent;
  121.                         }
  122.                         break;
  123.                     default:
  124.                         break;
  125.                 }
  126.             }
  127.         }
  128.     }
  129. }
  130.